!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
subroutine write_vtr_data(file_unit, var_name, var, x, y, z, nx, ny, nz, n_var, var_dim, format)
!==================================================================================================================================|

   implicit none

   integer(4), parameter :: n_dim = 3
   integer(4), parameter :: n_point_cell = 8

   character(*), intent(in) :: var_name(n_var)
   integer(4), intent(in) :: nx, ny, nz, n_var, format, file_unit

   integer(4), intent(in) :: var_dim(n_var)
   real(4), intent(in) :: x(nx), y(ny), z(nz)
   real(4), intent(in) :: var(sum(var_dim), nx, ny, nz)

   character(16) :: int_num1, int_num2, int_num3, int_num4, int_num5, int_num6

   integer(4) :: i_var, index_var

   integer(4) :: n_point, n_cell

!----------------------------------------------------------------------------------------------------------------------------------|
!  prepare
!----------------------------------------------------------------------------------------------------------------------------------|
   n_point = nx * ny * nz
   n_cell = (nx - 1) * (ny - 1) * (nz - 1)

!----------------------------------------------------------------------------------------------------------------------------------|
!  binary
!----------------------------------------------------------------------------------------------------------------------------------|
   if (format .eq. 0) then
!      index_var = 1
!      do i_var = 1, n_var
!!----------------------------------------------------------------------------------------------------------------------------------|
!!        write the scalar point data
!!----------------------------------------------------------------------------------------------------------------------------------|
!         if (var_dim(i_var) .eq. 1) then
!            write(file_unit) n_point * float_size, var(index_var, :, :, :)
!            index_var = index_var + var_dim(i_var)
!!----------------------------------------------------------------------------------------------------------------------------------|
!!        write the vector point data
!!----------------------------------------------------------------------------------------------------------------------------------|
!         elseif (var_dim(i_var) .eq. 3) then
!            write(file_unit) 3 * n_point * float_size, var(index_var : index_var + 2, :, :, :)
!            index_var = index_var + var_dim(i_var)
!         endif
!      enddo
!      write(file_unit) 3 * n_point * float_size, point
!      write(file_unit) n_point_cell * n_cell * int_size, connectivity
!      write(file_unit) n_cell * int_size, offset
!      write(file_unit) n_cell * int_size, cell_type

!----------------------------------------------------------------------------------------------------------------------------------|
!  ASCII
!----------------------------------------------------------------------------------------------------------------------------------|
   else
!----------------------------------------------------------------------------------------------------------------------------------|
!     write the info data
!----------------------------------------------------------------------------------------------------------------------------------|
      write(int_num1, '(es16.6)') minval(x)
      write(int_num2, '(es16.6)') maxval(x)
      write(int_num3, '(es16.6)') minval(y)
      write(int_num4, '(es16.6)') maxval(y)
      write(int_num5, '(es16.6)') minval(z)
      write(int_num6, '(es16.6)') maxval(z)
      write(file_unit, '(a)') '<Piece Extent="' //                                                                                 &
            int_num1 // ' ' // int_num2 // ' ' // int_num3 // ' ' // int_num4 // ' ' // int_num5 // ' ' // int_num6 // '">'

!      write(file_unit, '(a)') '<PointData>'
!      index_var = 1
!      do i_var = 1, n_var
!!----------------------------------------------------------------------------------------------------------------------------------|
!!        write the scalar point data
!!----------------------------------------------------------------------------------------------------------------------------------|
!         if (var_dim(i_var) .eq. 1) then
!            write(file_unit, '(a)') '<DataArray type="Float32" format="ascii" NumberOfComponents="1" Name="' //                    &
!               trim(var_name(i_var)) // '">'
!            write(file_unit, '(6es)') var(index_var, :, :, :)
!            write(file_unit, '(a)') '</DataArray>'
!            index_var = index_var + var_dim(i_var)
!!----------------------------------------------------------------------------------------------------------------------------------|
!!        write the vector point data
!!----------------------------------------------------------------------------------------------------------------------------------|
!         elseif (var_dim(i_var) .eq. 3) then
!            write(file_unit, '(a)') '<DataArray type="Float32" format="ascii" NumberOfComponents="3" Name="' //                    &
!               trim(var_name(i_var)) // '">'
!            write(file_unit, '(6es)') var(index_var : index_var + 2, :, :, :)
!            write(file_unit, '(a)') '</DataArray>'
!            index_var = index_var + var_dim(i_var)
!         endif
!      enddo
!      write(file_unit, '(a)') '</PointData>'

      write(file_unit, '(a)') '<Coodinates>'
      write(file_unit, '(a)') '<DataArray type="Float32" format="ascii" NumberOfComponents="1" Name="x">'
      write(file_unit, '(6es)') x
      write(file_unit, '(a)') '</DataArray>'
      write(file_unit, '(a)') '<DataArray type="Float32" format="ascii" NumberOfComponents="1" Name="y">'
      write(file_unit, '(6es)') y
      write(file_unit, '(a)') '</DataArray>'
      write(file_unit, '(a)') '<DataArray type="Float32" format="ascii" NumberOfComponents="1" Name="z">'
      write(file_unit, '(6es)') z
      write(file_unit, '(a)') '</DataArray>'
      write(file_unit, '(a)') '</Coodinates>'
      write(file_unit, '(a)') '</Piece>'
   endif

!----------------------------------------------------------------------------------------------------------------------------------|
   return
end subroutine write_vtr_data
